package com.archive.core.util;


import com.archive.core.constants.ArchiveIntervalType;
import com.archive.core.table.ArchiveTable;
import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class ArchiveUtil {

    /**
     * 获取归档时间戳
     *
     * @param archiveInterval     归档周期
     * @param archiveIntervalType 归档周期类型
     * @return 归档时间戳
     */
    public static long getArchiveTimeStamp(Integer archiveInterval, ArchiveIntervalType archiveIntervalType) {
        LocalDate localDate = LocalDate.now();
        if (ArchiveIntervalType.DAY == archiveIntervalType) {
            localDate = localDate.minusDays(archiveInterval);
        } else {
            localDate = localDate.minusMonths(archiveInterval);
        }
        return localDate.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    }

    public static String formatTime(long timestamp) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss:SSS");
        return Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
    }

    public static String formatTime(long timestamp, String pattern) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return Instant.ofEpochMilli(timestamp).atZone(ZoneId.systemDefault()).toLocalDateTime().format(formatter);
    }

    public static boolean checkTimeout(ArchiveTable archiveTable, List<ArchiveTable> archiveTables, long startTime, int archiveTimeout) {
        return archiveTimeout > 0 && archiveTables.contains(archiveTable)
                && SystemClock.now() > (startTime + (archiveTimeout * 60L * 1000));
    }

    public static void closeResultSet(ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            log.error(e.getMessage(), e);
        }
    }

    public static void closeStatement(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    public static void closeConnection(Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) {
        closeConnection(connection);
        closeStatement(statement);
        closeResultSet(resultSet);
    }

    public static void close(Statement statement, ResultSet resultSet) {
        closeStatement(statement);
        closeResultSet(resultSet);
    }

    public static Matcher getMatcher(String input) {
        Pattern pattern = Pattern.compile("#\\{\\s*(extract\\.\\w+(\\.(\\w+))?)\\s*}");
        return pattern.matcher(input);
    }

}
